package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallPriceHistoryMapper;
import org.linlinjava.litemall.db.domain.LitemallPriceHistory;
import org.linlinjava.litemall.db.domain.LitemallPriceHistoryExample;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallPriceHistoryService {
    @Resource
    private LitemallPriceHistoryMapper historyMapper;

    @Autowired
    private LitemallUserService userService;

    public final static Long CONVERSION_TYPE = 1l;//兑换
    public final static Long SHOP_TYPE = 2l; //购物
    public final static Long REFUND_TYPE = 3l; //退还

    public List<LitemallPriceHistory> queryIndex() {
        LitemallPriceHistoryExample example = new LitemallPriceHistoryExample();
        example.or().andDeletedEqualTo(false);
        return historyMapper.selectByExample(example);
    }

    public List<LitemallPriceHistory> querySelective(Integer userId, Integer page, Integer limit, String sort, String order) {
        LitemallPriceHistoryExample example = new LitemallPriceHistoryExample();
        LitemallPriceHistoryExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andMemberIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }


        PageHelper.startPage(page, limit);
        return historyMapper.selectByExample(example);
    }

    public int updateById(LitemallPriceHistory priceHistory) {
        priceHistory.setUpdateTime(LocalDateTime.now());
        return historyMapper.updateByPrimaryKeySelective(priceHistory);
    }

    public void deleteById(Integer id) {
        historyMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallPriceHistory priceHistory) {
        priceHistory.setAddTime(LocalDateTime.now());
        priceHistory.setUpdateTime(LocalDateTime.now());
        historyMapper.insertSelective(priceHistory);
    }

    public LitemallPriceHistory findById(Integer id) {
        return historyMapper.selectByPrimaryKey(id);
    }


    public void addBalanceToUser(LitemallUser user, String relationId, BigDecimal money, Long historyType){
        LitemallPriceHistory priceHistory = new LitemallPriceHistory();
        priceHistory.setMemberId(user.getId());

        priceHistory.setHistoryType(historyType);
        priceHistory.setRelationId(relationId);
        priceHistory.setMallPrice(money);
        add(priceHistory);
        user.setMallMoney(user.getMallMoney().add(money));

        userService.updateById(user);
    }

    public String getTypeString(LitemallPriceHistory history){
        long priceType = history.getHistoryType();
        if(priceType == CONVERSION_TYPE){
            return "绑定礼品卡(订单号:" + history.getRelationId() + ")";
        }
        if(priceType == SHOP_TYPE){
            return "购买商品(订单号:" + history.getRelationId() + ")";
        }
        if(priceType == REFUND_TYPE){
            return "退款";
        }
        return "其他操作";

    }
}
